<!DOCTYPE html>
<html lang="zh-CN">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>层叠、优先级和继承 | Cellinlab Blog</title>
    <meta name="generator" content="VuePress 1.9.7">
    <script src="https://cdn.jsdelivr.net/npm/react/umd/react.production.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/react-dom/umd/react-dom.production.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/@babel/standalone/babel.min.js"></script>
    <meta name="description" content="Notes about Web Programming &amp; Development.">
    
    <link rel="preload" href="/cellinlab-web-blog/assets/css/0.styles.0d80b6b1.css" as="style"><link rel="preload" href="/cellinlab-web-blog/assets/js/app.042abc34.js" as="script"><link rel="preload" href="/cellinlab-web-blog/assets/js/3.86f55582.js" as="script"><link rel="preload" href="/cellinlab-web-blog/assets/js/1.3029763b.js" as="script"><link rel="preload" href="/cellinlab-web-blog/assets/js/14.d7a6ba34.js" as="script"><link rel="prefetch" href="/cellinlab-web-blog/assets/js/10.a4352f92.js"><link rel="prefetch" href="/cellinlab-web-blog/assets/js/11.7cec8cf0.js"><link rel="prefetch" href="/cellinlab-web-blog/assets/js/12.fd862223.js"><link rel="prefetch" href="/cellinlab-web-blog/assets/js/13.30d1f0ba.js"><link rel="prefetch" href="/cellinlab-web-blog/assets/js/15.fc25211b.js"><link rel="prefetch" href="/cellinlab-web-blog/assets/js/16.93011c16.js"><link rel="prefetch" href="/cellinlab-web-blog/assets/js/4.d748b0d1.js"><link rel="prefetch" href="/cellinlab-web-blog/assets/js/5.0bb11173.js"><link rel="prefetch" href="/cellinlab-web-blog/assets/js/6.e8029cec.js"><link rel="prefetch" href="/cellinlab-web-blog/assets/js/7.0c2f68d4.js"><link rel="prefetch" href="/cellinlab-web-blog/assets/js/8.fe38b6cb.js"><link rel="prefetch" href="/cellinlab-web-blog/assets/js/9.960bedae.js">
    <link rel="stylesheet" href="/cellinlab-web-blog/assets/css/0.styles.0d80b6b1.css">
  </head>
  <body>
    <div id="app" data-server-rendered="true"><div class="theme-container no-sidebar" data-v-130b300a><div data-v-130b300a><div class="password-shadow password-wrapper-out" style="display:none;" data-v-25ba6db2 data-v-130b300a data-v-130b300a><h3 class="title" data-v-25ba6db2 data-v-25ba6db2>Cellinlab Blog</h3> <p class="description" data-v-25ba6db2 data-v-25ba6db2>Notes about Web Programming &amp; Development.</p> <label id="box" class="inputBox" data-v-25ba6db2 data-v-25ba6db2><input type="password" value="" data-v-25ba6db2> <span data-v-25ba6db2>Konck! Knock!</span> <button data-v-25ba6db2>OK</button></label> <div class="footer" data-v-25ba6db2 data-v-25ba6db2><span data-v-25ba6db2><i class="iconfont reco-theme" data-v-25ba6db2></i> <a target="blank" href="https://vuepress-theme-reco.recoluan.com" data-v-25ba6db2>vuePress-theme-reco</a></span> <span data-v-25ba6db2><i class="iconfont reco-copyright" data-v-25ba6db2></i> <a data-v-25ba6db2><span data-v-25ba6db2>Cell</span>
            
          <span data-v-25ba6db2>2019 - </span>
          2022
        </a></span></div></div> <div class="hide" data-v-130b300a><header class="navbar" data-v-130b300a><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/cellinlab-web-blog/" class="home-link router-link-active"><!----> <span class="site-name">Cellinlab Blog</span></a> <div class="links"><div class="color-picker"><a class="color-button"><i class="iconfont reco-color"></i></a> <div class="color-picker-menu" style="display:none;"><div class="mode-options"><h4 class="title">Choose mode</h4> <ul class="color-mode-options"><li class="dark">dark</li><li class="auto active">auto</li><li class="light">light</li></ul></div></div></div> <div class="search-box"><i class="iconfont reco-search"></i> <input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><a href="/cellinlab-web-blog/timeline/" class="nav-link"><i class="iconfont reco-date"></i>
  TimeLine
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-category"></i>
      分类
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/cellinlab-web-blog/categories/前端/" class="nav-link"><i class="undefined"></i>
  前端
</a></li></ul></div></div><div class="nav-item"><a href="/cellinlab-web-blog/tag/" class="nav-link"><i class="iconfont reco-tag"></i>
  标签
</a></div> <!----></nav></div></header> <div class="sidebar-mask" data-v-130b300a></div> <aside class="sidebar" data-v-130b300a><div class="personal-info-wrapper" data-v-39576ba9 data-v-130b300a><img src="/cellinlab-web-blog/avatar_h.png" alt="author-avatar" class="personal-img" data-v-39576ba9> <h3 class="name" data-v-39576ba9>
    Cell
  </h3> <div class="num" data-v-39576ba9><div data-v-39576ba9><h3 data-v-39576ba9>4</h3> <h6 data-v-39576ba9>文章</h6></div> <div data-v-39576ba9><h3 data-v-39576ba9>2</h3> <h6 data-v-39576ba9>标签</h6></div></div> <ul class="social-links" data-v-39576ba9><li class="social-item" data-v-39576ba9><i class="iconfont reco-github" style="color:#f47e60;" data-v-39576ba9></i></li><li class="social-item" data-v-39576ba9><i class="iconfont reco-mayun" style="color:#3498db;" data-v-39576ba9></i></li><li class="social-item" data-v-39576ba9><i class="iconfont reco-npm" style="color:#f47e60;" data-v-39576ba9></i></li><li class="social-item" data-v-39576ba9><i class="iconfont reco-twitter" style="color:#67cc86;" data-v-39576ba9></i></li><li class="social-item" data-v-39576ba9><i class="iconfont reco-bilibili" style="color:#abbd81;" data-v-39576ba9></i></li><li class="social-item" data-v-39576ba9><i class="iconfont reco-juejin" style="color:#fb9b5f;" data-v-39576ba9></i></li><li class="social-item" data-v-39576ba9><i class="iconfont reco-zhihu" style="color:#3498db;" data-v-39576ba9></i></li><li class="social-item" data-v-39576ba9><i class="iconfont reco-wechat" style="color:#f8b26a;" data-v-39576ba9></i></li><li class="social-item" data-v-39576ba9><i class="iconfont icon-gongzhonghao" style="color:#f47e60;" data-v-39576ba9></i></li></ul> <hr data-v-39576ba9></div> <nav class="nav-links"><div class="nav-item"><a href="/cellinlab-web-blog/timeline/" class="nav-link"><i class="iconfont reco-date"></i>
  TimeLine
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-category"></i>
      分类
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/cellinlab-web-blog/categories/前端/" class="nav-link"><i class="undefined"></i>
  前端
</a></li></ul></div></div><div class="nav-item"><a href="/cellinlab-web-blog/tag/" class="nav-link"><i class="iconfont reco-tag"></i>
  标签
</a></div> <!----></nav> <!----> </aside> <div class="password-shadow password-wrapper-in" style="display:none;" data-v-25ba6db2 data-v-130b300a><h3 class="title" data-v-25ba6db2 data-v-25ba6db2>层叠、优先级和继承</h3> <!----> <label id="box" class="inputBox" data-v-25ba6db2 data-v-25ba6db2><input type="password" value="" data-v-25ba6db2> <span data-v-25ba6db2>Konck! Knock!</span> <button data-v-25ba6db2>OK</button></label> <div class="footer" data-v-25ba6db2 data-v-25ba6db2><span data-v-25ba6db2><i class="iconfont reco-theme" data-v-25ba6db2></i> <a target="blank" href="https://vuepress-theme-reco.recoluan.com" data-v-25ba6db2>vuePress-theme-reco</a></span> <span data-v-25ba6db2><i class="iconfont reco-copyright" data-v-25ba6db2></i> <a data-v-25ba6db2><span data-v-25ba6db2>Cell</span>
            
          <span data-v-25ba6db2>2019 - </span>
          2022
        </a></span></div></div> <div data-v-130b300a><main class="page"><section><div class="page-title"><h1 class="title">层叠、优先级和继承</h1> <div data-v-f875f3fc><i class="iconfont reco-account" data-v-f875f3fc><span data-v-f875f3fc>Cell</span></i> <i class="iconfont reco-date" data-v-f875f3fc><span data-v-f875f3fc>2022-3-24</span></i> <!----> <i class="tags iconfont reco-tag" data-v-f875f3fc><span class="tag-item" data-v-f875f3fc>CSS</span><span class="tag-item" data-v-f875f3fc>样式优先级</span></i></div></div> <div class="theme-reco-content content__default"><h2 id="层叠"><a href="#层叠" class="header-anchor">#</a> 层叠</h2> <p>CSS 本质上就是声明规则，即在各种条件下，我们希望产生特定的效果。如果某个元素有这个类，则应用这些样式。要想预测规则最终的效果，就需要理解 CSS 里的层叠。</p> <div data-config="" data-type="vanilla" data-code="%3Chtml%3E%0A%20%20%3Cheader%20id%3D%22demo0-header%22%3E%0A%20%20%20%20%3Ch1%20id%3D%22demo0-page-title%22%20class%3D%22demo0-title%22%3EWombat%20Coffee%20Roasters%3C%2Fh1%3E%0A%20%20%20%20%3Cnav%3E%0A%20%20%20%20%20%20%3Cul%20id%3D%22demo0-main-nav%22%20class%3D%22demo0-nav%22%3E%0A%20%20%20%20%20%20%20%20%3Cli%3E%3Ca%20href%3D%22%2F%22%3EHome%3C%2Fa%3E%3C%2Fli%3E%0A%20%20%20%20%20%20%20%20%3Cli%3E%3Ca%20href%3D%22%2Fcoffees%22%3ECoffees%3C%2Fa%3E%3C%2Fli%3E%0A%20%20%20%20%20%20%20%20%3Cli%3E%3Ca%20href%3D%22%2Fbrewers%22%3EBrewers%3C%2Fa%3E%3C%2Fli%3E%0A%20%20%20%20%20%20%20%20%3Cli%3E%3Ca%20href%3D%22%2Fspecials%22%20class%3D%22demo0-featured%22%3ESpecials%3C%2Fa%3E%3C%2Fli%3E%0A%20%20%20%20%20%20%3C%2Ful%3E%0A%20%20%20%20%3C%2Fnav%3E%0A%20%20%3C%2Fheader%3E%0A%3C%2Fhtml%3E%0A%3Cscript%3E%0A%3C%2Fscript%3E%0A%3Cstyle%3E%0A%23demo0-header%20h1%20%7B%0A%20%20font-family%3A%20serif%3B%0A%7D%0A%23demo0-header%20%23demo0-page-title%20%7B%0A%20%20font-family%3A%20sans-serif%3B%0A%7D%0A%23demo0-header%20.demo0-title%20%7B%0A%20%20font-family%3A%20monospace%3B%0A%7D%0A%3C%2Fstyle%3E%0A" class="vuepress-plugin-demo-block__wrapper" style="display:none;"><div class="vuepress-plugin-demo-block__display"><div class="vuepress-plugin-demo-block__app"></div></div> <div class="vuepress-plugin-demo-block__code"><div class="language-html line-numbers-mode"><pre class="language-html"><code><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>html</span><span class="token punctuation">&gt;</span></span>
  <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>header</span> <span class="token attr-name">id</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>demo0-header<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>h1</span> <span class="token attr-name">id</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>demo0-page-title<span class="token punctuation">&quot;</span></span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>demo0-title<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>Wombat Coffee Roasters<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>h1</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>nav</span><span class="token punctuation">&gt;</span></span>
      <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>ul</span> <span class="token attr-name">id</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>demo0-main-nav<span class="token punctuation">&quot;</span></span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>demo0-nav<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>li</span><span class="token punctuation">&gt;</span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>a</span> <span class="token attr-name">href</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>/<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>Home<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>a</span><span class="token punctuation">&gt;</span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>li</span><span class="token punctuation">&gt;</span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>li</span><span class="token punctuation">&gt;</span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>a</span> <span class="token attr-name">href</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>/coffees<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>Coffees<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>a</span><span class="token punctuation">&gt;</span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>li</span><span class="token punctuation">&gt;</span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>li</span><span class="token punctuation">&gt;</span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>a</span> <span class="token attr-name">href</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>/brewers<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>Brewers<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>a</span><span class="token punctuation">&gt;</span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>li</span><span class="token punctuation">&gt;</span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>li</span><span class="token punctuation">&gt;</span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>a</span> <span class="token attr-name">href</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>/specials<span class="token punctuation">&quot;</span></span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>demo0-featured<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>Specials<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>a</span><span class="token punctuation">&gt;</span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>li</span><span class="token punctuation">&gt;</span></span>
      <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>ul</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>nav</span><span class="token punctuation">&gt;</span></span>
  <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>header</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>html</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>script</span><span class="token punctuation">&gt;</span></span><span class="token script"><span class="token language-javascript">
</span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>script</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>style</span><span class="token punctuation">&gt;</span></span><span class="token style"><span class="token language-css">
<span class="token selector">#demo0-header h1</span> <span class="token punctuation">{</span>
  <span class="token property">font-family</span><span class="token punctuation">:</span> serif<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token selector">#demo0-header #demo0-page-title</span> <span class="token punctuation">{</span>
  <span class="token property">font-family</span><span class="token punctuation">:</span> sans-serif<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token selector">#demo0-header .demo0-title</span> <span class="token punctuation">{</span>
  <span class="token property">font-family</span><span class="token punctuation">:</span> monospace<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>style</span><span class="token punctuation">&gt;</span></span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br></div></div></div> <div class="vuepress-plugin-demo-block__footer"></div></div> <p>层叠指的就是一系列规则。它决定了如何解决冲突，是CSS语言的基础。当声明冲突时，层叠会依据三种条件解决冲突：</p> <ol><li>样式表的来源：当声明冲突时，层叠会依据三种条件解决冲突。</li> <li>选择器优先级：哪些选择器比另一些选择器更重要。</li> <li>源码顺序：样式在样式表里的声明顺序。</li></ol> <p><img src="https://web-cellinlab-blog-1252726876.cos.ap-beijing.myqcloud.com/cascade-specificity-inheritance-%E5%B1%82%E5%8F%A0%E8%A7%84%E5%88%99.png" alt=""></p> <h3 id="样式表的来源"><a href="#样式表的来源" class="header-anchor">#</a> 样式表的来源</h3> <ul><li>作者样式
<ul><li>用于覆盖用户代理的样式</li></ul></li> <li>用户代理样式（即浏览器默认样式）
<ul><li>不同浏览器实现有差异</li></ul></li> <li><code>!important</code> 声明
<ul><li>标记 <code>!important</code> 会被当做更高优先级的来源</li></ul></li></ul> <p>因此，总体优先级由高到低排列：作者的 <code>!important</code> &gt; 作者 &gt; 用户代理。</p> <div data-config="" data-type="vanilla" data-code="%3Chtml%3E%0A%20%20%3Cheader%20id%3D%22demo1-header%22%3E%0A%20%20%20%20%3Ch1%20id%3D%22demo1-page-title%22%20class%3D%22demo1-title%22%3EWombat%20Coffee%20Roasters%3C%2Fh1%3E%0A%20%20%20%20%3Cnav%3E%0A%20%20%20%20%20%20%3Cul%20id%3D%22demo1-main-nav%22%20class%3D%22demo1-nav%22%3E%0A%20%20%20%20%20%20%20%20%3Cli%3E%3Ca%20href%3D%22%2F%22%3EHome%3C%2Fa%3E%3C%2Fli%3E%0A%20%20%20%20%20%20%20%20%3Cli%3E%3Ca%20href%3D%22%2Fcoffees%22%3ECoffees%3C%2Fa%3E%3C%2Fli%3E%0A%20%20%20%20%20%20%20%20%3Cli%3E%3Ca%20href%3D%22%2Fbrewers%22%3EBrewers%3C%2Fa%3E%3C%2Fli%3E%0A%20%20%20%20%20%20%20%20%3Cli%3E%3Ca%20href%3D%22%2Fspecials%22%20class%3D%22demo1-featured%22%3ESpecials%3C%2Fa%3E%3C%2Fli%3E%0A%20%20%20%20%20%20%3C%2Ful%3E%0A%20%20%20%20%3C%2Fnav%3E%0A%20%20%3C%2Fheader%3E%0A%3C%2Fhtml%3E%0A%3Cscript%3E%0A%3C%2Fscript%3E%0A%3Cstyle%3E%0A%23demo1-header%20h1%20%7B%0A%20%20color%3A%20%232f4f4f%3B%0A%20%20margin-bottom%3A%2010px%3B%0A%7D%0A%23demo1-main-nav%20%7B%0A%20%20margin-top%3A%2010px%3B%0A%20%20list-style%3A%20none%3B%0A%20%20padding-left%3A%200%3B%0A%7D%0A%23demo1-main-nav%20li%20%7B%0A%20%20display%3A%20inline-block%3B%0A%7D%0A%23demo1-main-nav%20a%20%7B%0A%20%20color%3A%20white%3B%0A%20%20background-color%3A%20%2313a4a4%3B%0A%20%20padding%3A%205px%3B%0A%20%20border-radius%3A%202px%3B%0A%20%20text-decoration%3A%20none%3B%0A%7D%0A%3C%2Fstyle%3E%0A" class="vuepress-plugin-demo-block__wrapper" style="display:none;"><div class="vuepress-plugin-demo-block__display"><div class="vuepress-plugin-demo-block__app"></div></div> <div class="vuepress-plugin-demo-block__code"><div class="language-html line-numbers-mode"><pre class="language-html"><code><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>html</span><span class="token punctuation">&gt;</span></span>
  <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>header</span> <span class="token attr-name">id</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>demo1-header<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>h1</span> <span class="token attr-name">id</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>demo1-page-title<span class="token punctuation">&quot;</span></span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>demo1-title<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>Wombat Coffee Roasters<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>h1</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>nav</span><span class="token punctuation">&gt;</span></span>
      <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>ul</span> <span class="token attr-name">id</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>demo1-main-nav<span class="token punctuation">&quot;</span></span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>demo1-nav<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>li</span><span class="token punctuation">&gt;</span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>a</span> <span class="token attr-name">href</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>/<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>Home<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>a</span><span class="token punctuation">&gt;</span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>li</span><span class="token punctuation">&gt;</span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>li</span><span class="token punctuation">&gt;</span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>a</span> <span class="token attr-name">href</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>/coffees<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>Coffees<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>a</span><span class="token punctuation">&gt;</span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>li</span><span class="token punctuation">&gt;</span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>li</span><span class="token punctuation">&gt;</span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>a</span> <span class="token attr-name">href</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>/brewers<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>Brewers<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>a</span><span class="token punctuation">&gt;</span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>li</span><span class="token punctuation">&gt;</span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>li</span><span class="token punctuation">&gt;</span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>a</span> <span class="token attr-name">href</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>/specials<span class="token punctuation">&quot;</span></span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>demo1-featured<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>Specials<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>a</span><span class="token punctuation">&gt;</span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>li</span><span class="token punctuation">&gt;</span></span>
      <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>ul</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>nav</span><span class="token punctuation">&gt;</span></span>
  <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>header</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>html</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>script</span><span class="token punctuation">&gt;</span></span><span class="token script"><span class="token language-javascript">
</span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>script</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>style</span><span class="token punctuation">&gt;</span></span><span class="token style"><span class="token language-css">
<span class="token selector">#demo1-header h1</span> <span class="token punctuation">{</span>
  <span class="token property">color</span><span class="token punctuation">:</span> #2f4f4f<span class="token punctuation">;</span>
  <span class="token property">margin-bottom</span><span class="token punctuation">:</span> 10px<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token selector">#demo1-main-nav</span> <span class="token punctuation">{</span>
  <span class="token property">margin-top</span><span class="token punctuation">:</span> 10px<span class="token punctuation">;</span>
  <span class="token property">list-style</span><span class="token punctuation">:</span> none<span class="token punctuation">;</span>
  <span class="token property">padding-left</span><span class="token punctuation">:</span> 0<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token selector">#demo1-main-nav li</span> <span class="token punctuation">{</span>
  <span class="token property">display</span><span class="token punctuation">:</span> inline-block<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token selector">#demo1-main-nav a</span> <span class="token punctuation">{</span>
  <span class="token property">color</span><span class="token punctuation">:</span> white<span class="token punctuation">;</span>
  <span class="token property">background-color</span><span class="token punctuation">:</span> #13a4a4<span class="token punctuation">;</span>
  <span class="token property">padding</span><span class="token punctuation">:</span> 5px<span class="token punctuation">;</span>
  <span class="token property">border-radius</span><span class="token punctuation">:</span> 2px<span class="token punctuation">;</span>
  <span class="token property">text-decoration</span><span class="token punctuation">:</span> none<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>style</span><span class="token punctuation">&gt;</span></span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br><span class="line-number">35</span><br><span class="line-number">36</span><br></div></div></div> <div class="vuepress-plugin-demo-block__footer"></div></div> <h3 id="理解优先级"><a href="#理解优先级" class="header-anchor">#</a> 理解优先级</h3> <p>浏览器将优先级分为两部分：HTML 的行内样式和选择器样式。</p> <ul><li>行内样式
<ul><li>用 HTML 的 style 属性写样式，这个声明只会作用于当前元素</li> <li>行内样式属于“带作用域的”声明，会覆盖任何来自样式表或者 <code>&lt;style&gt;</code> 标签的样式</li> <li>行内样式没有选择器，直接作用于所在的元素</li> <li>可以通过 <code>!important</code> 在样式表里覆盖行内声明</li></ul></li></ul> <div data-config="" data-type="vanilla" data-code="%3Chtml%3E%0A%20%20%3Cheader%20id%3D%22demo2-header%22%3E%0A%20%20%20%20%3Ch1%20id%3D%22demo2-page-title%22%20class%3D%22demo2-title%22%3EWombat%20Coffee%20Roasters%3C%2Fh1%3E%0A%20%20%20%20%3Cnav%3E%0A%20%20%20%20%20%20%3Cul%20id%3D%22demo2-main-nav%22%20class%3D%22demo2-nav%22%3E%0A%20%20%20%20%20%20%20%20%3Cli%3E%3Ca%20href%3D%22%2F%22%3EHome%3C%2Fa%3E%3C%2Fli%3E%0A%20%20%20%20%20%20%20%20%3Cli%3E%3Ca%20href%3D%22%2Fcoffees%22%3ECoffees%3C%2Fa%3E%3C%2Fli%3E%0A%20%20%20%20%20%20%20%20%3Cli%3E%3Ca%20href%3D%22%2Fbrewers%22%3EBrewers%3C%2Fa%3E%3C%2Fli%3E%0A%20%20%20%20%20%20%20%20%3Cli%3E%3Ca%20href%3D%22%2Fspecials%22%20class%3D%22demo2-featured%22%20style%3D%22background-color%3A%20orange%3B%22%3ESpecials%3C%2Fa%3E%3C%2Fli%3E%0A%20%20%20%20%20%20%3C%2Ful%3E%0A%20%20%20%20%3C%2Fnav%3E%0A%20%20%3C%2Fheader%3E%0A%3C%2Fhtml%3E%0A%3Cscript%3E%0A%3C%2Fscript%3E%0A%3Cstyle%3E%0A%23demo2-header%20h1%20%7B%0A%20%20color%3A%20%232f4f4f%3B%0A%20%20margin-bottom%3A%2010px%3B%0A%7D%0A%23demo2-main-nav%20%7B%0A%20%20margin-top%3A%2010px%3B%0A%20%20list-style%3A%20none%3B%0A%20%20padding-left%3A%200%3B%0A%7D%0A%23demo2-main-nav%20li%20%7B%0A%20%20display%3A%20inline-block%3B%0A%7D%0A%23demo2-main-nav%20a%20%7B%0A%20%20color%3A%20white%3B%0A%20%20background-color%3A%20%2313a4a4%3B%0A%20%20padding%3A%205px%3B%0A%20%20border-radius%3A%202px%3B%0A%20%20text-decoration%3A%20none%3B%0A%7D%0A%3C%2Fstyle%3E%0A" class="vuepress-plugin-demo-block__wrapper" style="display:none;"><div class="vuepress-plugin-demo-block__display"><div class="vuepress-plugin-demo-block__app"></div></div> <div class="vuepress-plugin-demo-block__code"><div class="language-html line-numbers-mode"><pre class="language-html"><code><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>html</span><span class="token punctuation">&gt;</span></span>
  <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>header</span> <span class="token attr-name">id</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>demo2-header<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>h1</span> <span class="token attr-name">id</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>demo2-page-title<span class="token punctuation">&quot;</span></span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>demo2-title<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>Wombat Coffee Roasters<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>h1</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>nav</span><span class="token punctuation">&gt;</span></span>
      <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>ul</span> <span class="token attr-name">id</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>demo2-main-nav<span class="token punctuation">&quot;</span></span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>demo2-nav<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>li</span><span class="token punctuation">&gt;</span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>a</span> <span class="token attr-name">href</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>/<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>Home<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>a</span><span class="token punctuation">&gt;</span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>li</span><span class="token punctuation">&gt;</span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>li</span><span class="token punctuation">&gt;</span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>a</span> <span class="token attr-name">href</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>/coffees<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>Coffees<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>a</span><span class="token punctuation">&gt;</span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>li</span><span class="token punctuation">&gt;</span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>li</span><span class="token punctuation">&gt;</span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>a</span> <span class="token attr-name">href</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>/brewers<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>Brewers<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>a</span><span class="token punctuation">&gt;</span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>li</span><span class="token punctuation">&gt;</span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>li</span><span class="token punctuation">&gt;</span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>a</span> <span class="token attr-name">href</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>/specials<span class="token punctuation">&quot;</span></span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>demo2-featured<span class="token punctuation">&quot;</span></span> <span class="token special-attr"><span class="token attr-name">style</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span><span class="token value css language-css"><span class="token property">background-color</span><span class="token punctuation">:</span> orange<span class="token punctuation">;</span></span><span class="token punctuation">&quot;</span></span></span><span class="token punctuation">&gt;</span></span>Specials<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>a</span><span class="token punctuation">&gt;</span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>li</span><span class="token punctuation">&gt;</span></span>
      <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>ul</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>nav</span><span class="token punctuation">&gt;</span></span>
  <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>header</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>html</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>script</span><span class="token punctuation">&gt;</span></span><span class="token script"><span class="token language-javascript">
</span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>script</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>style</span><span class="token punctuation">&gt;</span></span><span class="token style"><span class="token language-css">
<span class="token selector">#demo2-header h1</span> <span class="token punctuation">{</span>
  <span class="token property">color</span><span class="token punctuation">:</span> #2f4f4f<span class="token punctuation">;</span>
  <span class="token property">margin-bottom</span><span class="token punctuation">:</span> 10px<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token selector">#demo2-main-nav</span> <span class="token punctuation">{</span>
  <span class="token property">margin-top</span><span class="token punctuation">:</span> 10px<span class="token punctuation">;</span>
  <span class="token property">list-style</span><span class="token punctuation">:</span> none<span class="token punctuation">;</span>
  <span class="token property">padding-left</span><span class="token punctuation">:</span> 0<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token selector">#demo2-main-nav li</span> <span class="token punctuation">{</span>
  <span class="token property">display</span><span class="token punctuation">:</span> inline-block<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token selector">#demo2-main-nav a</span> <span class="token punctuation">{</span>
  <span class="token property">color</span><span class="token punctuation">:</span> white<span class="token punctuation">;</span>
  <span class="token property">background-color</span><span class="token punctuation">:</span> #13a4a4<span class="token punctuation">;</span>
  <span class="token property">padding</span><span class="token punctuation">:</span> 5px<span class="token punctuation">;</span>
  <span class="token property">border-radius</span><span class="token punctuation">:</span> 2px<span class="token punctuation">;</span>
  <span class="token property">text-decoration</span><span class="token punctuation">:</span> none<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>style</span><span class="token punctuation">&gt;</span></span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br><span class="line-number">35</span><br><span class="line-number">36</span><br></div></div></div> <div class="vuepress-plugin-demo-block__footer"></div></div> <ul><li><p>选择器优先级</p> <ul><li>id 选择器比类选择器优先级高</li> <li>类选择器比标签选择器高</li> <li>伪类选择器（如<code>:hover</code>）和属性选择器（如<code>[type=&quot;input&quot;]</code>）与一个类选择器的优先级相同</li> <li>通用选择器（<code>*</code>）和组合选择器（<code>&gt;</code>、<code>+</code>、<code>~</code>）对优先级没有影响</li></ul></li> <li><p>优先级标记
一个常用的表示优先级的方式是用数值形式来标记。如，<code>1,2,2</code> 表示选择器由 1 个 id、2 个类、2 个标签组成。</p></li></ul> <h3 id="源码顺序"><a href="#源码顺序" class="header-anchor">#</a> 源码顺序</h3> <p>如果两个声明的来源和优先级相同，出现晚的（包括在样式表出现较晚或者位于页面较晚引入的样式表中）声明胜出。</p> <h3 id="最佳实践"><a href="#最佳实践" class="header-anchor">#</a> 最佳实践</h3> <ul><li>在选择器中不要使用 id
<ul><li>当需要覆盖这个选择器时，通常找不到另一个有意义的 id，于是就会复制原来的选择器，然后加上另一个类</li></ul></li> <li>不要使用 <code>!important</code> <ul><li>比 id 更难覆盖，一旦使用，需要覆盖原先的声明，就需要再加上一个 <code>!important</code>，而且依然要处理优先级问题</li></ul></li> <li>创建一个用于分发的 JS 模块（如 NPM 包）时，强烈建议不要在 JS 里使用行内样式
<ul><li>否则，使用者要么全盘接受，要么动用 <code>!important</code> 覆盖</li> <li>正确的做法是在包里包含一个样式表。如果组件要频繁修改样式，可以通过 JS 给元素添加或者删除类来实现。</li></ul></li></ul> <h2 id="继承"><a href="#继承" class="header-anchor">#</a> 继承</h2> <p>如果一个元素的某个属性没有层叠值，则可能会继承某个祖先元素的值。继承属性从DOM树的父节点传递到后代节点。</p> <p>但不是所有的属性都能被继承。默认情况下，只有特定的一些属性能被继承，通常是我们希望被继承的那些。它们主要是跟文本相关的属性：<code>color</code>、<code>font</code>、<code>font-family</code>、<code>font-size</code>、<code>font-weight</code>、<code>font-variant</code>、<code>font-style</code>、<code>line-height</code>、<code>letter-spacing</code>、<code>text-align</code>、<code>text-indent</code>、<code>text-transform</code>、<code>white-space</code> 以及<code>word-spacing</code>。</p> <p>还有一些其他的属性也可以被继承，比如列表属性：<code>list-style</code>、<code>list-style-type</code>、<code>list-style-position</code> 以及 <code>list-style-image</code>。表格的边框属性<code>border-collapse</code> 和 <code>border-spacing</code> 也能被继承。</p> <h2 id="特殊值"><a href="#特殊值" class="header-anchor">#</a> 特殊值</h2> <ul><li><p><code>inherit</code> 关键字</p> <ul><li>用继承代替一个层叠值。可以用它来覆盖另一个值，这样该元素就会继承其父元素的值。</li> <li>还可以使用 <code>inherit</code> 关键字强制继承一个通常不会被继承的属性，比如边框和内边距。</li></ul></li> <li><p><code>initial</code> 关键字</p> <ul><li>撤销作用于某个元素的样式</li> <li>每一个 CSS 属性都有初始（默认）值。如果将 <code>initial</code> 值赋给某个属性，那么就会有效地将其重置为默认值，这种操作相当于硬复位了该值</li></ul></li></ul></div></section> <footer class="page-edit"><!----> <div class="last-updated"><span class="prefix">最近更新: </span> <span class="time">4 分钟前</span></div></footer> <!----> <div class="comments-wrapper"><!----></div> <ul class="side-bar sub-sidebar-wrapper" style="width:12rem;" data-v-cb1513f6><li class="level-2" data-v-cb1513f6><a href="/cellinlab-web-blog/pages/web/CSS/cascade-specificity-inheritance.html#层叠" class="sidebar-link reco-side-层叠" data-v-cb1513f6>层叠</a></li><li class="level-3" data-v-cb1513f6><a href="/cellinlab-web-blog/pages/web/CSS/cascade-specificity-inheritance.html#样式表的来源" class="sidebar-link reco-side-样式表的来源" data-v-cb1513f6>样式表的来源</a></li><li class="level-3" data-v-cb1513f6><a href="/cellinlab-web-blog/pages/web/CSS/cascade-specificity-inheritance.html#理解优先级" class="sidebar-link reco-side-理解优先级" data-v-cb1513f6>理解优先级</a></li><li class="level-3" data-v-cb1513f6><a href="/cellinlab-web-blog/pages/web/CSS/cascade-specificity-inheritance.html#源码顺序" class="sidebar-link reco-side-源码顺序" data-v-cb1513f6>源码顺序</a></li><li class="level-3" data-v-cb1513f6><a href="/cellinlab-web-blog/pages/web/CSS/cascade-specificity-inheritance.html#最佳实践" class="sidebar-link reco-side-最佳实践" data-v-cb1513f6>最佳实践</a></li><li class="level-2" data-v-cb1513f6><a href="/cellinlab-web-blog/pages/web/CSS/cascade-specificity-inheritance.html#继承" class="sidebar-link reco-side-继承" data-v-cb1513f6>继承</a></li><li class="level-2" data-v-cb1513f6><a href="/cellinlab-web-blog/pages/web/CSS/cascade-specificity-inheritance.html#特殊值" class="sidebar-link reco-side-特殊值" data-v-cb1513f6>特殊值</a></li></ul></main> <!----></div></div></div></div><div class="global-ui"><div class="back-to-ceiling" style="right:1rem;bottom:6rem;width:2.5rem;height:2.5rem;border-radius:.25rem;line-height:2.5rem;display:none;" data-v-c6073ba8 data-v-c6073ba8><svg t="1574745035067" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="5404" class="icon" data-v-c6073ba8><path d="M526.60727968 10.90185116a27.675 27.675 0 0 0-29.21455937 0c-131.36607665 82.28402758-218.69155461 228.01873535-218.69155402 394.07834331a462.20625001 462.20625001 0 0 0 5.36959153 69.94390903c1.00431239 6.55289093-0.34802892 13.13561351-3.76865779 18.80351572-32.63518765 54.11355614-51.75690182 118.55860487-51.7569018 187.94566865a371.06718723 371.06718723 0 0 0 11.50484808 91.98906777c6.53300375 25.50556257 41.68394495 28.14064038 52.69160883 4.22606766 17.37162448-37.73630017 42.14135425-72.50938081 72.80769204-103.21549295 2.18761121 3.04276886 4.15646224 6.24463696 6.40373557 9.22774369a1871.4375 1871.4375 0 0 0 140.04691725 5.34970492 1866.36093723 1866.36093723 0 0 0 140.04691723-5.34970492c2.24727335-2.98310674 4.21612437-6.18497483 6.3937923-9.2178004 30.66633723 30.70611158 55.4360664 65.4791928 72.80769147 103.21549355 11.00766384 23.91457269 46.15860503 21.27949489 52.69160879-4.22606768a371.15156223 371.15156223 0 0 0 11.514792-91.99901164c0-69.36717486-19.13165746-133.82216804-51.75690182-187.92578088-3.42062944-5.66790279-4.76302748-12.26056868-3.76865837-18.80351632a462.20625001 462.20625001 0 0 0 5.36959269-69.943909c-0.00994388-166.08943902-87.32547796-311.81420293-218.6915546-394.09823051zM605.93803103 357.87693858a93.93749974 93.93749974 0 1 1-187.89594924 6.1e-7 93.93749974 93.93749974 0 0 1 187.89594924-6.1e-7z" p-id="5405" data-v-c6073ba8></path><path d="M429.50777625 765.63860547C429.50777625 803.39355007 466.44236686 1000.39046097 512.00932183 1000.39046097c45.56695499 0 82.4922232-197.00623328 82.5015456-234.7518555 0-37.75494459-36.9345906-68.35043303-82.4922232-68.34111062-45.57627738-0.00932239-82.52019037 30.59548842-82.51086798 68.34111062z" p-id="5406" data-v-c6073ba8></path></svg></div></div></div>
    <script src="/cellinlab-web-blog/assets/js/app.042abc34.js" defer></script><script src="/cellinlab-web-blog/assets/js/3.86f55582.js" defer></script><script src="/cellinlab-web-blog/assets/js/1.3029763b.js" defer></script><script src="/cellinlab-web-blog/assets/js/14.d7a6ba34.js" defer></script>
  </body>
</html>
